//
//  SearchRangeViewController.m
//  Double
//
//  Created by 袁庆秋 on 15/5/28.
//  Copyright (c) 2015年 袁庆秋. All rights reserved.
//

#import "SearchRangeViewController.h"
#import <AFNetworking.h>
#import <UIImageView+WebCache.h>
#import "TopicCell.h"
#import "TopicModel.h"
#import "SearchCell.h"
#import "SearchModel.h"
#import "CartoonRecommendDetailViewController.h"
#import "MJRefresh.h"

@interface SearchRangeViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, assign) NSInteger pageCount;

@end

@implementation SearchRangeViewController

- (void)dealloc
{
    [_tableArray release];
    [_table release];
    [_searchText release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
        self.pageCount = 2;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = self.searchText;
    [self startConnection];
    [self createView];
}

- (void)startConnection
{
    NSString *str = [NSString stringWithFormat:@"http://manhua.haomee.cn/?m=Cartoon&ios=23&pad=0&a=search&word=%@&page=1&limit=20&type=0", self.searchText];
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *array = [dic objectForKey:@"list"];
        for (NSDictionary *dic in array) {
            SearchModel *model = [[SearchModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.tableArray addObject:model];
        }
        [self.table reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

#pragma mark -
#pragma mark tableView
- (void)createView
{
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.table.rowHeight = 180;
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    [self.table release];
}


- (void)tableRefresh
{
    [self.tableArray removeAllObjects];
    [self startConnection];
    [self.table reloadData];
    [self.table.header endRefreshing];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tableArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdenti = @"cell";
    SearchCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdenti];
    if (!cell) {
        cell = [[SearchCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdenti];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    SearchModel *model = [self.tableArray objectAtIndex:indexPath.row];
    cell.nameLabel.text = model.name;
    [cell.showImage sd_setImageWithURL:[NSURL URLWithString:model.cover] placeholderImage:[UIImage imageNamed:@"placeholder.jpg"] options:SDWebImageRetryFailed];
    cell.scoreLabel.text = [NSString stringWithFormat:@"%@分", model.score];
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CartoonRecommendDetailViewController *detail = [[CartoonRecommendDetailViewController alloc] init];
    SearchModel *model = [self.tableArray objectAtIndex:indexPath.row];
    detail.iD = model.myId;
    detail.lastup = model.cur_num;
    detail.titleName = model.name;
    [self.navigationController pushViewController:detail animated:YES];
}








@end
